package hikari.gui.panels.particle;

import com.gooddies.events.ValueChangedEvent;
import com.gooddies.swing.RowLayout;
import com.gooddies.texteditors.DefaultFloatTextField;
import hikari.gui.guicomponents.hLabel;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import org.swig.jirr.vector3df;

/**
 * @author sad
 */
public class Vector3dEditor {

    private DefaultFloatTextField x;
    private DefaultFloatTextField y;
    private DefaultFloatTextField z;
    private ActionListener valueChanged;
    private boolean disableInput = false;

    public Vector3dEditor(JPanel panel) {
        int width = 40;
        panel.add(new hLabel("X:"));
        x = new DefaultFloatTextField(-9999999, 9999999);
        x.setPreferredSize(new Dimension(width, 20));
        panel.add(x);
        x.setValueChangedEventNumber(getAction());
        panel.add(new hLabel("Y:"));
        y = new DefaultFloatTextField(-99999999, 99999999);
        y.setPreferredSize(new Dimension(width, 20));
        y.setAllowNegative(true);
        panel.add(y);
        y.setValueChangedEventNumber(getAction());
        panel.add(new hLabel("Z:"));
        z = new DefaultFloatTextField(-999999999, 999999999);
        z.setPreferredSize(new Dimension(width, 20));
        z.setAllowNegative(true);
        panel.add(z, RowLayout.NewRow);
        z.setValueChangedEventNumber(getAction());
    }

    public void setValue(vector3df data) {
        disableInput = true;
        x.setValue(data.getX());
        y.setValue(data.getY());
        z.setValue(data.getZ());
        disableInput = false;
    }

    public vector3df getValue() {
        return new vector3df((float) x.getValue(), (float) y.getValue(), (float) z.getValue());
    }

    public void setValueChangedEvent(ActionListener valueChanged) {
        this.valueChanged = valueChanged;
    }

    private ValueChangedEvent<Float> getAction() {
        return new ValueChangedEvent<Float>() {
            @Override
            public void valueChanged(Float value) {
                if (valueChanged != null && disableInput == false) {
                    valueChanged.actionPerformed(null);
                }
            }
        };
    }
}
